package online.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import online.entity.UserDao;
import online.mapper.SystemUserMapper;
import online.mapper.SystemUserMybatisplusMapper;
import online.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: 狼芒
 * @Date: 2021/9/12 19:43
 * @Descrintion: 继承 ServiceImpl<SystemUserMybatisplusMapper, UserDao>，可以继承很多方法
 * @version: 1.0
 */
@Service
public class LoginServiceImpl extends ServiceImpl<SystemUserMybatisplusMapper, UserDao> implements LoginService {

    @Autowired
    SystemUserMapper systemUserMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public UserDao getUser(Long id) {
        if (redisTemplate.hasKey(id)) {
            return (UserDao) redisTemplate.opsForValue().get(id);
        }
        UserDao user = systemUserMapper.getUser(id);
        redisTemplate.opsForValue().set(id, user);
        return user;
    }

    @Override
    public List<UserDao> getAllUser() {
        if (redisTemplate.hasKey("all")) {
            return (List<UserDao>) redisTemplate.opsForValue().get("all");
        }
        counter.increment();
        List<UserDao> allUser = systemUserMapper.getAllUser();
        redisTemplate.opsForValue().set("all", allUser);
        return allUser;
    }

    @Autowired
    SystemUserMybatisplusMapper systemUserMybatisplusMapper;

    @Override
    public UserDao getMybatisPlusUser() {
//        Wrapper<UserDao> quryMapper = new Wrapper<UserDao>();
        return systemUserMybatisplusMapper.selectById(1);
    }

    Counter counter;

    /**
     * 自定义统计属性，还有其他相关属性，可以进行探索
     *
     * @param meterRegistry
     */
    public LoginServiceImpl(MeterRegistry meterRegistry) {
        counter = meterRegistry.counter("userServices.getAllUser.count");
    }
}
